<template>
  <div class="exam-container">
    <!-- 开始界面 -->
    <div v-if="!started" class="start-screen">
      <div class="glassmorphism-card">
        <h1 class="title">法律职业资格测评系统</h1>
        <button @click="startQuestion" class="start-button pulse">
          <i class="fas fa-balance-scale button-icon"></i>
          <span class="button-text">开始测评</span>
          <div class="hover-effect"></div>
        </button>
        <div class="decorative-line"></div>
      </div>
    </div>

    <!-- 测评界面 -->
    <transition v-else name="question-transition" mode="out-in">
      <div v-if="!showReport" key="questions" class="question-screen">
        <!-- 头部进度 -->
        <div class="progress-header">
          <div class="progress-track">
            <div
              class="progress-bar"
              :style="{
                width: `${(currentQuestionIndex / questions.length) * 100}%`,
              }"
            ></div>
          </div>
          <div class="question-meta">
            <span>第 {{ currentQuestionIndex + 1 }} 题</span>
            <div class="accuracy-progress">
              <div class="progress-label">正确率</div>
              <div class="progress-container">
                <div
                  class="progress-fill"
                  :style="{ width: `${correctRate}%` }"
                ></div>
                <span class="progress-text">{{ correctRate }}%</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 题目区域 -->
        <div class="question-card">
          <h2 class="question-title">{{ currentQuestion.title }}</h2>

          <!-- 选项区域 -->
          <div class="options-grid">
            <div
              v-for="(option, index) in currentQuestion.options"
              :key="index"
              class="option-card"
              :class="getOptionClass(option)"
              @click="selectAnswer(option)"
            >
              <div class="option-content">
                <div class="option-id">{{ option.id }}</div>
                <div class="option-text">{{ option.text }}</div>
              </div>
              <div class="status-indicator">
                <i
                  v-if="showExplanation && option.correct"
                  class="fas fa-check"
                ></i>
                <i
                  v-if="selectedAnswer === option.id && !option.correct"
                  class="fas fa-times"
                ></i>
              </div>
            </div>
          </div>

          <transition name="fade">
            <button
              v-if="selectedAnswer && !showExplanation"
              @click="submitAnswer"
              class="submit-button gradient-button"
            >
              <i class="fas fa-paper-plane"></i>
              提交答案
            </button>
          </transition>
        </div>

        <!-- 答案解析 -->
        <transition name="explanation-slide">
          <div v-if="showExplanation" class="explanation-card">
            <h3 class="explanation-title">
              <i class="fas fa-lightbulb"></i>
              题目解析
            </h3>
            <p class="explanation-text">{{ currentQuestion.explanation }}</p>
            <button class="next-button gradient-button" @click="nextQuestion">
              {{ isLastQuestion ? "生成报告" : "下一题" }}
            </button>
          </div>
        </transition>
      </div>
      <!-- 在transition中添加报告界面 -->
      <div v-else>
        <transition name="report-slide">
          <div v-if="showReport" class="report-screen">
            <div class="report-container">
              <div class="report-header">
                <i class="fas fa-gavel"></i>
                <h1>{{ userName }}的法律执业能力评估报告</h1>
                <div class="seal-mark"></div>
              </div>

              <div class="core-stats">
                <div class="stat-card primary-stat">
                  <div class="stat-icon">
                    <i class="fas fa-star"></i>
                  </div>
                  <div class="stat-content">
                    <div class="stat-value">{{ score }}</div>
                    <div class="stat-label">执业能力指数</div>
                    <div class="stat-subtext">满分 {{ totalScore }}</div>
                  </div>
                </div>

                <div class="stat-grid">
                  <div class="stat-item">
                    <i class="fas fa-check-circle"></i>
                    <div>
                      <div class="value">
                        {{ correctCount }}/{{ questions.length }}
                      </div>
                      <div class="label">正确题数</div>
                    </div>
                  </div>
                  <div class="stat-item">
                    <i class="fas fa-clock"></i>
                    <div>
                      <div class="value">{{ elapsedTime }}</div>
                      <div class="label">答题用时</div>
                    </div>
                  </div>
                  <div class="stat-item highlight">
                    <i class="fas fa-chart-line"></i>
                    <div>
                      <div class="value">{{ correctRate }}%</div>
                      <div class="label">正确率</div>
                    </div>
                  </div>
                </div>
              </div>

              <button class="restart-button" @click="resetExam">
                <i class="fas fa-redo"></i>
                重新测评
              </button>
            </div>
          </div>
        </transition>
      </div>
    </transition>
  </div>
</template>

<script>
import api from "@/api";

export default {
  data() {
    return {
      started: false,
      currentQuestionIndex: 0,
      selectedAnswer: null,
      showExplanation: false,
      score: 0,
      correctCount: 0,
      startTime: null,
      elapsedTime: "0:00",
      showReport: false,
      userName: "王小宝",
      totalScore: "40",
      questions: [
        {
          title: "根据《民法典》，下列哪项不属于民事权利？",
          options: [
            { id: "A", text: "生命权", correct: true },
            { id: "B", text: "名誉权", correct: false },
            { id: "C", text: "行政权", correct: false },
            { id: "D", text: "肖像权", correct: false },
          ],
          explanation:
            "《民法典》第一百一十条规定：自然人享有生命权、身体权、健康权、姓名权、肖像权、名誉权...",
        },
        {
          title: "一加一等于几？",
          options: [
            { id: "A", text: "生命权", correct: true },
            { id: "B", text: "名誉权", correct: false },
            { id: "C", text: "行政权", correct: false },
            { id: "D", text: "肖像权", correct: false },
          ],
          explanation: "二",
        },
        // 其他题目...
      ],
      userInfo: "",
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.userInfo = JSON.parse(localStorage.getItem("userInfo"));
    });
  },
  computed: {
    currentQuestion() {
      return this.questions[this.currentQuestionIndex];
    },
    isLastQuestion() {
      return this.currentQuestionIndex === this.questions.length - 1;
    },
    correctRate() {
      return Math.round((this.correctCount / this.questions.length) * 100);
    },
  },
  methods: {
    async startQuestion() {
      try {
        const result = await api.auth.start({
          person_id: this.userInfo.id,
        });
        console.log(result.data, "------");
        this.started = true;
        this.startTime = Date.now();
        this.timer = setInterval(this.updateTimer, 1000);
      } catch (error) {
        console.error("请求失败:", error.message);
      }
    },
    selectAnswer(option) {
      if (this.showExplanation) return;

      this.selectedAnswer = option.id;
      if (option.correct) {
        this.score += 30; // 每题30分
        this.correctCount++;
      }

      this.showExplanation = true;
    },
    submitAnswer() {
      const selectedOption = this.currentQuestion.options.find(
        (opt) => opt.id === this.selectedAnswer
      );
      if (selectedOption.correct) {
        this.score += 30;
        this.correctCount++;
      }
      this.showExplanation = true;
    },
    nextQuestion() {
      if (!this.isLastQuestion) {
        this.currentQuestionIndex++;
        this.selectedAnswer = null;
        this.showExplanation = false;
      } else {
        clearInterval(this.timer);
        this.showReport = true;
      }
    },
    getOptionClass(option) {
      return {
        selected: this.selectedAnswer === option.id,
        correct: this.showExplanation && option.correct,
        incorrect: this.selectedAnswer === option.id && !option.correct,
      };
    },
    updateTimer() {
      const seconds = Math.floor((Date.now() - this.startTime) / 1000);
      this.elapsedTime = `${Math.floor(seconds / 60)}:${String(
        seconds % 60
      ).padStart(2, "0")}`;
    },
    resetExam() {
      Object.assign(this.$data, this.$options.data());
    },
  },
};
</script>

<style>
@import url("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css");
</style>

<style lang="scss" scoped>
$primary: #4361ee;
$success: #06d6a0;
$error: #ef476f;
$text: #2b2d42;

.exam-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  padding: 1rem;
  box-sizing: border-box;
}

// 开始界面样式
.start-screen {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;

  .glassmorphism-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(12px);
    border-radius: 24px;
    padding: 3rem 4rem;
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.1);
    text-align: center;

    width: 650px; // 默认PC端宽度
    max-width: 90%; // 移动端最大宽度
    @media (max-width: 768px) {
      padding: 1.5rem 2rem;
      width: 90%;
      border-radius: 16px;

      .title {
        font-size: 1.8rem; // 移动端标题缩小
      }
    }

    @media (max-width: 480px) {
      padding: 1rem;
      .title {
        font-size: 1.5rem;
      }
    }
  }
}

// 题目卡片样式
.question-card {
  background: white;
  border-radius: 20px;
  padding: 2.5rem;
  margin-top: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
}

// 选项网格布局
.options-grid {
  display: grid;
  gap: 1rem;
  margin-top: 2rem;
}

.option-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.2rem;
  border: 2px solid #e0e6ed;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  &.selected {
    border-color: $primary;
    background: rgba($primary, 0.05);
  }

  &.correct {
    border-color: $success;
    background: rgba($success, 0.05);
  }

  &.incorrect {
    border-color: $error;
    background: rgba($error, 0.05);
  }
}

// 报告界面样式
.report-card {
  position: relative;
  padding: 3rem 4rem;
  max-width: 800px;
  margin: 2rem auto;
  background: rgba(white, 0.96);
  border: 2px solid rgba($primary, 0.1);
  &::before {
    content: "";
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 96%;
    height: 12px;
    background: linear-gradient(135deg, $primary 0%, $success 100%);
    border-radius: 0 0 6px 6px;
    opacity: 0.2;
  }
}

// 动画效果
.question-transition-enter-active,
.question-transition-leave-active {
  transition: all 0.5s ease;
}

.question-transition-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.question-transition-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

.explanation-slide-enter-active {
  transition: all 0.3s ease-out;
}

.explanation-slide-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.start-button {
  position: relative;
  padding: 1.5rem 3rem;
  border: none;
  border-radius: 50px;
  background: linear-gradient(135deg, $success 0%, $primary 100%);
  box-shadow: 0 8px 24px rgba($primary, 0.3);
  color: white;
  font-size: 1.2rem;
  cursor: pointer;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  @media (max-width: 768px) {
    padding: 1rem 2rem;
    font-size: 1rem;

    .button-icon {
      font-size: 1.2rem;
    }
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 32px rgba($primary, 0.4);

    .button-icon {
      opacity: 0.9;
    }
  }

  &:active {
    transform: scale(0.98);
  }
}
.hover-effect {
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    45deg,
    transparent 25%,
    rgba($primary, 0.15) 50%,
    transparent 75%
  );
  animation: hover-glow 3s infinite linear;
  opacity: 0.3;
}
@keyframes hover-glow {
  0% {
    transform: translateX(-25%) rotate(45deg);
  }
  100% {
    transform: translateX(25%) rotate(45deg);
  }
}

// 修改文字样式
.button-text {
  position: relative;
  z-index: 1;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

// 调整图标样式
.button-icon {
  filter: drop-shadow(0 2px 4px rgba($primary, 0.2));
  transition: opacity 0.3s;
}

.report-title {
  display: flex;
  align-items: center;
  gap: 1rem;

  i {
    font-size: 2.5rem;
    color: #4361ee;
  }
}

// 更新进度条样式
.progress-header {
  position: relative;
  padding-left: 50px;
  display: flex; // 添加flex布局
  align-items: center; // 垂直居中
  justify-content: space-between; // 分散对齐
  height: 60px; // 固定高度

  &::before {
    content: "\f0e3";
    font-family: "Font Awesome 5 Free";
    position: absolute;
    left: 10px; // 调整定位位置
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.8rem;
    color: $primary;
    font-weight: 900; // 添加字体权重
  }
}

// 增强选项卡片的图标反馈
.option-card {
  .status-indicator {
    i {
      &.fa-check {
        color: $success;
        background: rgba($success, 0.1);
        padding: 5px;
        border-radius: 50%;
      }
      &.fa-times {
        color: $error;
        background: rgba($error, 0.1);
        padding: 5px;
        border-radius: 50%;
      }
    }
  }
}

// 添加法律主题水印
.exam-container::after {
  content: "\f0f6";
  font-family: "Font Awesome 5 Free";
  position: fixed;
  bottom: 20px;
  right: 20px;
  font-size: 6rem;
  color: rgba(67, 97, 238, 0.05);
  z-index: -1;
}

.question-meta {
  display: flex;
  align-items: center; // 确保垂直居中
  gap: 2rem;
  height: 100%; // 添加高度继承

  .accuracy-progress {
    flex-grow: 1;
    min-width: 200px;
  }
}

.analysis-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 2rem;
  margin-top: 3rem;
}

.analysis-card {
  padding: 1.5rem;
  background: rgba(white, 0.95);
  border-radius: 15px;
  border: 1px solid rgba($primary, 0.1);

  h3 {
    color: $text;
    margin-bottom: 1rem;

    i {
      color: $primary;
      margin-right: 8px;
    }
  }
}

.rank-progress {
  height: 8px;
  background: rgba($primary, 0.1);
  border-radius: 4px;
  overflow: hidden;

  .progress-fill {
    height: 100%;
    background: linear-gradient(90deg, $success 0%, $primary 100%);
    position: relative;

    span {
      position: absolute;
      right: 8px;
      top: 12px;
      font-size: 0.8em;
      color: $primary;
    }
  }
}

// 添加印章动画
@keyframes stamp-animation {
  0% {
    transform: scale(3) rotate(15deg);
    opacity: 0;
  }
  100% {
    transform: scale(1) rotate(15deg);
    opacity: 0.6;
  }
}

.progress-container {
  position: relative;
  height: 24px;
  width: 100%; // 添加宽度设置
  background: rgba($primary, 0.1);
  border-radius: 12px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, $success 0%, $primary 100%);
  border-radius: 12px;
  transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}
.progress-text {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: white;
  font-size: 0.9em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.progress-label {
  font-size: 0.9em;
  color: $text;
  margin-bottom: 4px;
}

// 在样式表中添加以下内容
.gradient-button {
  position: relative;
  padding: 1rem 2.5rem;
  border: none;
  border-radius: 30px;
  background: linear-gradient(135deg, $success 0%, $primary 100%);
  color: white;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 15px rgba($primary, 0.3);
  overflow: hidden;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba($primary, 0.4);

    &::after {
      opacity: 0.1;
    }
  }

  &:active {
    transform: scale(0.98);
  }

  &::after {
    content: "";
    position: absolute;
    top: 0;
    left: -100%;
    width: 200%;
    height: 100%;
    background: linear-gradient(
      to right,
      transparent 25%,
      rgba(255, 255, 255, 0.2) 50%,
      transparent 75%
    );
    transition: opacity 0.3s;
    opacity: 0;
    animation: shine 3s infinite;
  }
}

@keyframes shine {
  100% {
    left: 100%;
  }
}

.explanation-card {
  button {
    margin-top: 1.5rem;
  }
}

.report-container {
  position: relative;
  background: white;
  border-radius: 20px;
  padding: 1rem;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  // max-width: 800px;
  // width: 90%;
}

.report-header {
  text-align: center;
  margin-bottom: 2rem;
  position: relative;

  i.fa-gavel {
    font-size: 2.8rem;
    background: linear-gradient(135deg, $primary 30%, $success 70%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    filter: drop-shadow(0 2px 4px rgba($primary, 0.3));
    transition: all 0.3s ease;
    position: relative;
    padding: 8px;

    &::after {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border: 2px solid rgba($primary, 0.15);
      border-radius: 8px;
      transform: rotate(5deg);
    }

    @media (max-width: 768px) {
      font-size: 2rem;
      padding: 6px;
    }
  }

  h1:hover i.fa-gavel {
    transform: rotate(-15deg) scale(1.1);
    filter: drop-shadow(0 4px 8px rgba($primary, 0.4));
  }

  h1 {
    color: $text;
    font-size: 2rem;
    margin: 1rem 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;

    i {
      color: $primary;
      font-size: 1.8rem;
    }
  }
}

.core-stats {
  display: grid;
  gap: 2rem;
}

.primary-stat {
  display: flex;
  align-items: center;
  gap: 2rem;
  padding: 2rem;
  background: linear-gradient(
    135deg,
    rgba($primary, 0.08) 0%,
    rgba($success, 0.08) 100%
  );
  border-radius: 18px;

  .stat-icon {
    width: 100px;
    height: 100px;
    background: $primary;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;

    i {
      color: white;
      font-size: 2.5rem;
    }
  }

  .stat-value {
    font-size: 3.5rem;
    font-weight: bold;
    color: $primary;
  }

  .stat-label {
    font-size: 1.2rem;
    color: $text;
    margin-bottom: 0.5rem;
  }

  .stat-subtext {
    color: rgba($text, 0.7);
  }
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;

  .stat-item {
    padding: 1.5rem;
    background: rgba($primary, 0.03);
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 1rem;

    i {
      font-size: 1.8rem;
      color: $primary;
    }

    .value {
      font-size: 1.5rem;
      font-weight: bold;
      color: $text;
    }

    .label {
      color: rgba($text, 0.8);
      font-size: 0.9rem;
    }

    &.highlight {
      background: rgba($success, 0.08);
      i {
        color: $success;
      }
    }
  }
}

.restart-button {
  margin-top: 2rem;
  width: 100%;
  padding: 1.2rem;
  background: $primary;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1.1rem;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    background: darken($primary, 5%);
    box-shadow: 0 5px 15px rgba($primary, 0.3);
  }

  i {
    margin-right: 0.8rem;
  }
}
</style>